package org.com.coolfish.service;
import java.util.List;
import org.com.coolfish.common.database.entity.KuyuAddPackage;
import org.com.coolfish.common.database.entity.KuyuOrderFlow;
import org.com.coolfish.common.database.service.KuyuAddPackageService;
import org.com.coolfish.common.database.service.KuyuOperatorService;
import org.com.coolfish.common.database.service.KuyuOrderFlowService;
import org.com.coolfish.common.database.service.KuyuPackageListService;
import org.com.coolfish.common.database.service.KuyuPackageService;
import org.com.coolfish.common.message.MsisdnCardMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * @author HS
 *	订单支付成功，里面却没有订购记录的
 *	需要去订购上去,将订单记录的状态改过来
 */
@Service
public class CheckOrderPackageService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private KuyuAddPackageService kuyuAddPackageService;
	@Autowired
	private KuyuOrderFlowService kuyuOrderFlowService;
	@Autowired
	private OrderPackageService orderPackageService;

	public void checkOrder() {
		List<KuyuOrderFlow> kuyuOrderFlowList = kuyuOrderFlowService.findOrderByStatus();
		MsisdnCardMessage message = null;
		KuyuAddPackage kuyuAddPackage = null;
		for (KuyuOrderFlow kuyuOrderFlow : kuyuOrderFlowList) {
			
			 kuyuAddPackage = kuyuAddPackageService.findOrderByCardIdAndPackageid(kuyuOrderFlow.getCardid(),kuyuOrderFlow.getPackageid());
			
			//套餐后台未进行订购操作
			//订单支付成功，里面却没有订购记录的
			if (null == kuyuAddPackage) {
				
				logger.info("号码未生成订购套餐包：[{}]",kuyuAddPackage);
				message = new MsisdnCardMessage();
				message.setPackageid(kuyuOrderFlow.getPackageid());
				message.setCardid(kuyuOrderFlow.getCardid());
				message.setOperatorid(kuyuOrderFlow.getOperatorid());
				message.setIccid(kuyuOrderFlow.getIccid());
				
				//生成一个相应的包
				orderPackageService.saveCycleWithPackageProperty(message);
				//修改状态
				kuyuOrderFlowService.changeStatusOfKuyuOrderFlow(kuyuOrderFlow.getCardid());
			}else {
				logger.info("号码[{}],有生成订购套餐包[{}]：", kuyuAddPackage.getCard(),kuyuAddPackage);
			}
			
		}
	}
	


}
